<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * 分页类文件
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id $
 * @package libraries
 */
/**
 * 分页类
 * @package libraries
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
class TPage
{
	/**
	 * 总页数
	 * @var int
	 */
	private $pagecount;
	/**
	 * uri assoc
	 * @var object
	 */
	private $uri_assoc;
	/**
	 * 数据库 limit 内容
	 * @var strying
	 */
	private $limit;
	private $recordcount;
	private $pagenum;
	private $perpage;
	private $uri;
	private $ar = array("[首页]","[上页]","[下页]","[尾页]");
	private $config;
	private $page_flag = 'page';
	private $uri_string = '';
	private $pagestr;

	/** 构造函数 */
	public function __construct()
	{
		$this->config = Configure::item_array();
		if ($this->config['enable_query_strings'] === false)
		{
			$this->uri			= TBase::get_instance()->uri;
			$this->uri_string	= $this->uri->uri_string();
			preg_match_all( "@\/" . $this->page_flag . ",(\d{1,}|\/)@is",$this->uri_string,$tmp);
			$this->pagenum		= isset($tmp[1][0])?$tmp[1][0]:1;
			$this->pagestr		= isset($tmp[0][0])?$tmp[0][0]:'';
		}
	}
	/**
	 * 初始化函数
	 * @param int $recordcount
	 * @param int $pagesize
	 * @param int $default
	 * @param int $perpage
	 */
	public function initialize($recordcount=200, $pagesize = 20, $default='-', $perpage = 10)
	{
		$this->recordcount 	= $recordcount;
		$this->perpage 		= $perpage;
		$this->pagecount 	= ceil($recordcount/$pagesize);
		$this->pagenum 		= $this->_currentpage();
		$this->offset		= ($this->pagenum-1)*$pagesize;
		$this->limit		= $pagesize;
	}
	private function _currentpage()
	{
		if ($this->config['enable_query_strings'] === TRUE && isset($_GET[$this->page_flag]) === TRUE)
		{
			$this->pagenum = isset($_GET[$this->page_flag])?intval($_GET[$this->page_flag]):1;
		}
		if($this->pagenum <= 0)
		{
			return 1;
		}
		else if($this->pagenum > $this->pagecount)
		{
			return $this->pagecount;
		}
		else
		{
			return  $this->pagenum;
		}
		return 1;
	}
	/**
	 * URL重组,自动判断是否开启请求字符串方式
	 * @return string
	 */
	private function _newarg($num=1)
	{
		if ($this->config['enable_query_strings'] === false)
		{
			$page_flag	= "/".$this->page_flag.",".$num;
			$uri_string = $this->uri_string;

			if($this->pagestr!='')
			{
				$uri_string = preg_replace("@\/page,\d{1,}@is",$page_flag, $this->uri_string );
			}
			else
			{
				$uri_string .= $page_flag;
			}
			return Configure::site_url($uri_string);
		}
		else
		{
			$urlar	= $_GET;
			$str	= '';
			unset($urlar[$this->page_flag]);
			if (!isset($urlar[$this->config['controller_trigger']]))
			{
				$urlar[$this->config['controller_trigger']] = $this->config['default_controller'];
			}
			if($urlar)
			{
				foreach($urlar as $key=>$val)
				{
					$str .= "?$key=$val";
				}
				$str .= "&$this->page_flag=".$num;
			}
			return $this->config['base_url'].'/'.$str;
		}
	}

	/**
	 * 数据库 limit 字符串
	 * @return string
	 */
	public function limit($offset=false)
	{
		if($offset)	return array($this->limit,$this->offset);
		if(!$offset)return $this->limit;
	}
	public function offset()
	{
		return $this->offset;
	}
	/**
	 * 模板输出函数
	 * @param bool $sel
	 * @return string
	 */
	public function display()
	{
		$trunpage = "&nbsp;共[".$this->recordcount."]条数据/共[".$this->pagecount."]页&nbsp;";
		//=============================================================================================
		if($this->pagenum == 1)
		{
			$trunpage .= $this->ar[0]."&nbsp;".$this->ar[1]."&nbsp;";
		}
		else
		{
			$trunpage .= '<a href="'.$this->_newarg(1).'">'.$this->ar[0].'</a>&nbsp;<a href="'.$this->_newarg($this->pagenum-1).'">'.$this->ar[1].'</a>&nbsp;';
		}
		//=============================================================================================
		$numpage = 1;
		while(($this->pagenum - $this->perpage * $numpage) > 0)
		{
			$numpage++;
		}
		$startpage = ($numpage - 1) * $this->perpage;
		$endpage = $this->pagecount < $numpage * $this->perpage ? $this->pagecount : $numpage * $this->perpage;
		//=============================================================================================
		for($i = $startpage + 1; $i <= $endpage; $i++)
		{
			$trunpage .= $i != $this->pagenum ? "<a href=\"".$this->_newarg($i)."\">$i</a>&nbsp;" : "<strong>[".$i."]</strong>&nbsp;";
		}
		//=============================================================================================
		if($this->pagenum == $this->pagecount || $this->pagecount == 0)
		{
			$trunpage .= $this->ar[2]."&nbsp;".$this->ar[3]."&nbsp;";
		}
		else
		{
			$trunpage .= '<a href="'.$this->_newarg($this->pagenum+1).'">'.$this->ar[2].'</a>&nbsp;<a href="'.$this->_newarg($this->pagecount).'">'.$this->ar[3].'</a>&nbsp;';
		}
		return $trunpage;
	}
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Nov 07 23:55:52 CST 2007
+--------------------------------------------------------------------------
*/
?>